/*
 * Knapsack.java
 *
 * Created on January 6, 2008, 3:36 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package wisp.dataObjects;
import java.sql.*;
import java.io.*;
import java.util.*;
import java.util.Vector;
import java.lang.Exception.*;
/**
 *
 * @author Nezie Santos
 */
public class Knapsack {
     private dbConnection dbcon = new dbConnection();
     private String iName, cName, itype;
     private int sNum, quantity, weight, isEquiped;
    /** Creates a new instance of Knapsack */
    public Knapsack() {
    }
    public Knapsack (String character, String item, int slot, int quant, int wt, String ittype, int ise) {
        setCName(character);
        setIName(item);
        setSNum(slot);
        setQuantity(quant);
        setWeight(wt);
        itype = ittype;
        isEquiped = ise;
    }
    public void addKnapsack (String cname)
    {
      	String query; 	
        PreparedStatement stmt;
        getDbcon().connect();
      
        for (int i = 1; i<=30; i++)//paedit kay nezie kung gising na..:D
        {
            query = "INSERT INTO Knapsack (characterName, itemName, itemQuantity, slotNumber, weight, itype, isEquiped) VALUES (?,?,?,?,?,?,?)";
            try 
            {
                stmt = getDbcon().con.prepareStatement(query);
                stmt.setString(1, cname);
                stmt.setString(2, "none");
                stmt.setInt(3, 0);
                stmt.setInt(4, i);
                stmt.setInt(5, 0);
                stmt.setString(6, "none");
                stmt.setInt(7, 0);
                stmt.executeUpdate();	
                stmt.close();
            } catch (Exception ex) 
            {
                System.out.println(ex.getMessage());
            }
            
        }
        getDbcon().disconnect();
    }
    
    public boolean checkExist(String cname, String iname) 
    {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        boolean exist = false;
                
        getDbcon().connect();
        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, weight, isEquiped FROM Knapsack WHERE characterName = ? AND itemName = ?";
      
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cname);
            stmt.setString(2, iname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                exist = true;
                this.itype = rs.getString("itype");
                this.setCName(rs.getString("characterName"));
                this.setIName(rs.getString("itemName"));
                this.setQuantity(rs.getInt("itemQuantity"));
                this.setSNum(rs.getInt("slotNumber"));
                this.setWeight(rs.getInt("weight"));
                this.isEquiped = rs.getInt("isEquiped");
            }
            rs.close(); 					
            stmt.close();					
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
        return exist;
    }
    
   public Vector viewKnapsack (String name) 
   {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Knapsack> kList;
            
        kList = new Vector<Knapsack>();
                
        getDbcon().connect();
        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, weight, isEquiped FROM Knapsack WHERE characterName = ? ORDER BY slotNumber ASC";
         
        try
        {
             stmt = getDbcon().con.prepareStatement(query);
             stmt.setString(1, name);
             rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Knapsack k = new Knapsack();
                k.itype = rs.getString("itype");
                k.setCName(rs.getString("characterName"));
                k.setIName(rs.getString("itemName"));
                k.setQuantity(rs.getInt("itemQuantity"));
                k.setSNum(rs.getInt("slotNumber"));
                k.setWeight(rs.getInt("weight"));
                k.isEquiped = rs.getInt("isEquiped");
                kList.add(k);
               
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return kList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
   
   public Knapsack[] viewKnapsaArray (String name) 
   {
        Knapsack[] kn = new Knapsack[30];
        
        for (int i=1; i<=30; i++)
        {
            Knapsack k = new Knapsack();
            k.viewSlot(name, i);
            kn[i] = k;
        }
        return kn;
   }
   
    public Vector viewEquip (String name) {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
        Vector<Knapsack> kList;
            
        kList = new Vector<Knapsack>();
        Iterator it =kList.iterator();
                
        getDbcon().connect();
        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, weight, isEquiped FROM Knapsack WHERE characterName = ? ORDER BY slotNumber ASC";
         
        try
        {
             stmt = getDbcon().con.prepareStatement(query);
             stmt.setString(1, name);
             rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                Knapsack k = new Knapsack();
                k.itype = rs.getString("itype");
                k.setCName(rs.getString("characterName"));
                k.setIName(rs.getString("itemName"));
                k.setQuantity(rs.getInt("itemQuantity"));
                k.setSNum(rs.getInt("slotNumber"));
                k.setWeight(rs.getInt("weight"));
                k.isEquiped = rs.getInt("isEquiped");
                kList.add(k);
               
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            return kList;
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
        return null;
     }
    public void viewEmptySlot (String name) {
        //Statement stmt;
        String query;
        ResultSet rs;
        PreparedStatement stmt;
       
                
        getDbcon().connect();
        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, weight, isEquiped FROM Knapsack WHERE characterName = ? AND itemName = ?";
         
        try
        {
             stmt = getDbcon().con.prepareStatement(query);
             stmt.setString(1, name);
             stmt.setString(2, "none");
             rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {	
                this.itype = rs.getString("itype");
                this.setCName(rs.getString("characterName"));
                this.setIName(rs.getString("itemName"));
                this.setQuantity(rs.getInt("itemQuantity"));
                this.setSNum(rs.getInt("slotNumber"));
                this.setWeight(rs.getInt("weight"));
                this.isEquiped = rs.getInt("isEquiped");
               
             }
            //crset.populate(rs);
            rs.close(); 					
            stmt.close();
            
 				
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
       finally {
            getDbcon().disconnect();
        }
     
     }
     public void viewSlot (String cname, int snum){
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, weight, isEquiped FROM Knapsack WHERE characterName = ? AND slotNumber = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cname);
            stmt.setInt(2, snum);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.itype = rs.getString("itype");
                this.setCName(rs.getString("characterName"));
                this.setIName(rs.getString("itemName"));
                this.setQuantity(rs.getInt("itemQuantity"));
                this.setSNum(rs.getInt("slotNumber"));
                this.setWeight(rs.getInt("weight"));
                this.isEquiped = rs.getInt("isEquiped");
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
 
        }
    }
     public void viewItem (String cname, String iname)
     {
        String query;
        ResultSet rs;
        PreparedStatement stmt;
                
        getDbcon().connect();
        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, weight, isEquiped FROM Knapsack WHERE characterName = ? AND itemName = ?";
        
        try
        {
            stmt = getDbcon().con.prepareStatement(query);
            stmt.setString(1, cname);
            stmt.setString(2, iname);
            rs = stmt.executeQuery();	
        
            while (rs.next()) 
            {
                this.itype = rs.getString("itype");
                this.setCName(rs.getString("characterName"));
                this.setIName(rs.getString("itemName"));
                this.setQuantity(rs.getInt("itemQuantity"));
                this.setSNum(rs.getInt("slotNumber"));
                this.setWeight(rs.getInt("weight"));
                this.isEquiped = rs.getInt("isEquiped");
            }
            rs.close(); 					
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println(ex.getMessage());	       	
        }
        finally {
            getDbcon().disconnect();
 
        }
    }

     public void editKnapsack (String character, String item, int slot, int val, int wt, String tp, int ise){
        
        String query; 					
        ResultSet rs;
        PreparedStatement stmt;
        getDbcon().connect();
        query = "UPDATE Knapsack SET itemName = ?, itype=?, itemQuantity = ?, weight = ?, isEquiped = ? WHERE characterName = ? AND slotNumber = ?";
        try 
        {
            stmt = getDbcon().con.prepareStatement(query);
            
            if (val <= 0)
            {
                stmt.setString(1, "none");
                stmt.setString(2, "none");
                stmt.setInt(3, 0);
                stmt.setInt(4, 0);
                stmt.setInt(5, 0);
                stmt.setString(6, character);
                stmt.setInt(7, slot);
            }
            else
            {
                stmt.setString(1, item);
                stmt.setString(2, tp);
                stmt.setInt(3, val);
                stmt.setInt(4, wt);
                stmt.setInt(5, ise);
                stmt.setString(6, character);
                stmt.setInt(7, slot);
            }
            stmt.executeUpdate();	
            stmt.close();					
        } catch (Exception ex) 
        {
            System.out.println(ex.getMessage());	       	
        }
        getDbcon().disconnect();
    }


    public dbConnection getDbcon() {
        return dbcon;
    }

    public void setDbcon(dbConnection dbcon) {
        this.dbcon = dbcon;
    }

    public String getIName() {
        return iName;
    }

    public void setIName(String iName) {
        this.iName = iName;
    }

    public String getCName() {
        return cName;
    }

    public void setCName(String cName) {
        this.cName = cName;
    }

    public int getSNum() {
        return sNum;
    }

    public void setSNum(int sNum) {
        this.sNum = sNum;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getItype() {
        return itype;
    }

    public void setItype(String itype) {
        this.itype = itype;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getIsEquiped() {
        return isEquiped;
    }

    public void setIsEquiped(int isEquiped) {
        this.isEquiped = isEquiped;
    }
}

//      public boolean isHere (int isEquip, String cname){
//        String query;
//        ResultSet rs;
//        PreparedStatement stmt;
//        boolean isHere = false;
//                
//        getDbcon().connect();
//        query = "SELECT itype, characterName, itemName, itemQuantity, slotNumber, isEquiped FROM Knapsack WHERE characterName = ? AND isEquiped = ?";
//        
//        try
//        {
//            stmt = getDbcon().con.prepareStatement(query);
//            stmt.setString(1, cname);
//            stmt.setInt (2, isEquip);
//            rs = stmt.executeQuery();	
//        
//            while (rs.next()) 
//            {
//                this.itype = rs.getString("itype");
//                this.setCName(rs.getString("characterName"));
//                this.setIName(rs.getString("itemName"));
//                this.setQuantity(rs.getInt("itemQuantity"));
//                this.setSNum(rs.getInt("slotNumber"));
//                this.setIsEquiped(rs.getInt("isEquiped"));
//                isHere = true;
//            }
//            rs.close(); 					
//            stmt.close();
//        } catch (Exception ex)
//        {
//            System.out.println(ex.getMessage());	       	
//        }
//        finally {
//            getDbcon().disconnect();
//            return isHere;
//        }
//    }

//     public void editIsEquip (String character, String iname, int isEq){
//        
//        String query; 					
//        ResultSet rs;
//        PreparedStatement stmt;
//        getDbcon().connect();
//        query = "UPDATE Knapsack SET isEquiped = ? WHERE characterName = ? AND itemName = ?";
//        try 
//        {
//             
//            stmt = getDbcon().con.prepareStatement(query);       
//            stmt.setInt(1, isEq);
//            stmt.setString(2, character);
//            stmt.setString(3, iname);
//            stmt.executeUpdate();	
//            stmt.close();					
//        } catch (Exception ex) 
//        {
//            System.out.println(ex.getMessage());	       	
//        }
//        getDbcon().disconnect();
//    }
